package com.example.yang_android_seventh;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import static android.hardware.SensorManager.SENSOR_DELAY_GAME;

public class uc1 extends AppCompatActivity implements SensorEventListener {
    private ImageView image;
    float currentDegree = 0f;
    private SensorManager mSensorManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc1);
        image=findViewById(R.id.image);
        image.setImageResource(R.drawable.abcd);
        mSensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);

    }
    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SENSOR_DELAY_GAME);
    }
    @Override
    protected void onStop()
    {
        mSensorManager.unregisterListener(this);
        super.onStop();
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values=event.values;
        int sensorType=event.sensor.getType();
        StringBuilder sb=null;
        switch(sensorType) {
            case Sensor.TYPE_ORIENTATION:
                float degree=event.values[0];
                RotateAnimation ra = new RotateAnimation(currentDegree,
                        -degree, Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                ra.setDuration(200);
                image.startAnimation(ra);
                currentDegree=degree;
                break;
        }
        }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
